package com.bw.module_mine.model.Adpter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.lib_storage.db.entity.VideoStarEntity
import com.bw.module_mine.R
import com.bw.module_mine.databinding.ItemCollectBinding

class CollectionAdapter : RecyclerView.Adapter<CollectionAdapter.ViewHolder>(){

    interface OnItemClickListener{
        fun onItemClick(position:Int)
    }

    private var listener : OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.listener = listener
    }

    private val list = ArrayList<VideoStarEntity>()

    fun setData(mList:List<VideoStarEntity>){
        list.clear()
        list.addAll(mList)
        notifyDataSetChanged()
    }

    class ViewHolder(
        val binding:ItemCollectBinding
    ):RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCollectBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = list[position]
        holder.binding.collect = item

        Glide.with(holder.itemView.context)
            .load(item.videomainimag)
            .placeholder(R.drawable.ic_launcher_foreground)
            .error(R.drawable.ic_launcher_foreground)
            .into(holder.binding.ivIcon)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}